﻿using SalesSys.BLL;
using SalesSys.BLL.Purchasing;
using SalesSys.BLL.Sales;
using SalesSys.DAL.Models;
using SalesSys.Service.Purchasing;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http;


namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 负责人
    /// </summary>
    [RoutePrefix("api/Director")]
    public class DirectorController : BaseApiController
    {
        private OrderBLL bll = BllFactory.GetBll<OrderBLL>();
        private IPurchasingSvc purchasingSvc = new PurchasingSvc();

        /// <summary>
        /// 负责人-获取所辖客户经理计划单
        /// </summary>
        /// <param name="state">计划单状态</param>
        /// <param name="type">计划单类型</param>
        /// <param name="createTime">创建时间</param>
        /// <param name="name">供应商名称、客户名称、简称</param>
        /// <param name="page">分页</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        [Route("OrderList")]
        [HttpGet]
        public ViewDtos<Order> GetOrder(int page, int rows, OrderState? state, OrderType? type, DateTime? createTime = null, string name = null)
        {
            var empId = ClientHelper.ClientInfo.Id;
            //var paging = GetRequestQuery();
            var re = bll.GetDirectorOrder(empId, page, rows, state, type, createTime, name);
            var dto = new ViewDtos<Order>();
            dto.Data = re.Value;
            dto.Total = re.Key;
            dto.Page = page;
            dto.Rows = rows;
            return dto;
        }

        /// <summary>
        /// 获取采购单列表
        /// </summary>
        /// <param name="name">供应商名称、客户名称、简称</param>
        /// <param name="page">分页</param>
        /// <param name="rows">每页条数</param>
        /// <param name="state">采购单状态</param>
        /// <param name="createTime">查询时间</param>
        /// <returns></returns>
        [Route("PurchaseList")]
        [HttpGet]
        public ViewDtos<PurchaseOrder> GetPurchase(DateTime? createTime = null, PurchaseOrder.PurchaseState? state = null, string name = null, int page = 1, int rows = 10)
        {
            var data = purchasingSvc.GetDirectorList(name, createTime, state, ClientHelper.ClientInfo.Id, page, rows);
            var dto = ViewDto.CreateList(data.Value);
            dto.Total = data.Key;
            dto.Page = page;
            dto.Rows = 10;
            return dto;
        }

        #region 导出所辖客户经理计划单
        /// <summary>
        /// 导出所辖客户经理计划单
        /// </summary>
        /// <param name="state">计划单状态</param>
        /// <param name="type">计划单类型</param>
        /// <param name="createTime">查询时间</param>
        /// <param name="name">供应商名称、客户名称、简称</param>
        /// <returns></returns>
        [Route("ExpOrderList")]
        [HttpGet]
        public HttpResponseMessage ExpOrderList(OrderState? state, OrderType? type, DateTime? createTime = null, string name = null)
        {
            var empId = ClientHelper.ClientInfo.Id;
            //查询所有已结算
            var orderList = bll.GetDirectorOrder(empId, 1, 19999, state, type, createTime, name);

            HttpResponseMessage msg = null;
            var expData = new List<List<string>>();
            foreach (var ord in orderList.Value)
            {
                foreach (var commodity in ord.CommodityInfos)
                {
                    var data = new List<String>();
                    data.Add(ord.Customer.ShortName);
                    data.Add(ord.Creater.Name);
                    data.Add(ord.Provider.Name);
                    data.Add(ord.Total + "/" + ord.Weight);
                    data.Add((ord.IsFullPayment ? "先款" : "") + (ord.IsStandard ? "标件" : ""));
                    data.Add(ord.District.Name);
                    data.Add(ord.Remarks);
                    data.Add(ord.State.GetDescription());
                    data.Add(ord.CreateTime.ToString());
                    expData.Add(data);
                }
            }
            var nameArray = new string[] { "客户名", "客户经理", "供应商", "金额/重量t", "状态", "地址", "备注", "状态", "创建时间" };

            msg = CsvExp("所辖客户经理计划单.csv", expData, nameArray);
            return msg;
        }
        #endregion


        #region 导出所辖采购单列表
        /// <summary>
        /// 导出所辖采购单列表
        /// </summary>
        /// <param name="createTime">查询时间</param>
        /// <param name="state">计划单状态</param>
        /// <param name="name">供应商名称、客户名称、简称</param>
        /// <returns></returns>
        [Route("ExpPurchaseList")]
        [HttpGet]
        public HttpResponseMessage ExpPurchaseList(DateTime? createTime = null, PurchaseOrder.PurchaseState? state = null, string name = null)
        {
            var empId = ClientHelper.ClientInfo.Id;
            //查询所有已结算
            var orderList = purchasingSvc.GetDirectorList(name, createTime, state, ClientHelper.ClientInfo.Id, 1, 19999);

            HttpResponseMessage msg = null;
            var expData = new List<List<string>>();
            foreach (var ord in orderList.Value)
            {
                var data = new List<String>();
                data.Add(ord.CreateTime.ToString());
                data.Add(ord.Provider.Name);
                data.Add(ord.TotalWeight.ToString());
                data.Add(ord.Total.ToString());
                data.Add(ord.ToDepot != null ? ord.ToDepot.Name : "");
                data.Add(ord.Account);
                data.Add(ord.Creater.Name);
                data.Add(ord.Remarks);
                data.Add(ord.State.GetDescription());
                expData.Add(data);
            }
            var nameArray = new string[] { "日期", "供应商", "数量（t）", "金额（元）", "库房", "户头", "申请人", "备注", "状态" };

            msg = CsvExp("所辖采购单.csv", expData, nameArray);
            return msg;
        }
        #endregion
    }
}